package com.shaoshaohuo.app.ui.ec;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.AppConfig;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PurchaseDetail;
import com.shaoshaohuo.app.entity.PurchaseDetailEntity;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.view.TopbarView;

public class PurchaseRecordDetailActivity extends BaseActivity {

	private TopbarView mTopbarView;

	private TextView mProductText;
	private TextView mStandardText;
	private TextView mPriceText;
	private TextView mCountText;
	private TextView mAddressText;
	private TextView mContactNameText;
	private TextView mPhoneText;
	private TextView mTimeText;
	private TextView mYaoqiuText;
	
	private Button mNextButton;
	
	private String id;

	private PurchaseDetail data;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_purchase_record_detail);
		
		initData();
		initView();
		setUpView();
		requestData();
	}

	private void requestData() {
	    startLoadingDialog();
	    RequestService.getInstance().getPurchaseDetail(this, id, PurchaseDetailEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    PurchaseDetailEntity entity = (PurchaseDetailEntity) resultData;
                    updataUi(entity);
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }
//	 = (TextView) this.findViewById(R.id.textview_product);
//	 = (TextView) this.findViewById(R.id.textview_guige);
//	 = (TextView) this.findViewById(R.id.textview_price);
//	mCountText = (TextView) this.findViewById(R.id.textview_count);
//	mAddressText = (TextView) this.findViewById(R.id.textview_address);
//	mContactNameText = (TextView) this.findViewById(R.id.textview_contact_name);
//	mPhoneText = (TextView) this.findViewById(R.id.textview_contact_phone);
//	mTimeText = (TextView) this.findViewById(R.id.textview_time);
//	mYaoqiuText = (TextView) this.findViewById(R.id.textview_yaoqiu);
	protected void updataUi(PurchaseDetailEntity entity) {
		data = entity.getData();
		String title = "";
		if (!StringUtil.isEmpty(data.getCatname()) ) {
			title = data.getTitle() + " / " + data.getCatname();
		} else {
			title = data.getTitle();
		}
		mProductText.setText("购买产品：" + title);
		mStandardText.setText("规格："+data.getProductnot());
		mPriceText.setText("购买价格："+data.getPrice()+"元/"+data.getUnit());
		mCountText.setText("购买数量："+data.getNum()+data.getUnit());
		mAddressText.setText("收货地址："+ AreaUtil.getAreaFullname(data.getCityid()) + data.getAddress());
		mContactNameText.setText("联系人："+data.getRealname());
		mPhoneText.setText("联系电话：" + AppConfig.getPhone(data.getMobile(), data.getKey()));
		mTimeText.setText("购买时间："+data.getStart()+" - "+data.getEnd());
		mYaoqiuText.setText("购买要求："+data.getContent());
		
		mNextButton.setText("已有"+data.getQuotenum()+"人参与报价>>");
		if(Integer.parseInt(data.getQuotenum()) <=0){
			mNextButton.setEnabled(false);
		}else{
			mNextButton.setEnabled(true);
		}
	}

	private void initData() {
		id = getIntent().getStringExtra(ExtraName.id);
	}
	
	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mProductText = (TextView) this.findViewById(R.id.textview_product);
		mStandardText = (TextView) this.findViewById(R.id.textview_guige);
		mPriceText = (TextView) this.findViewById(R.id.textview_price);
		mCountText = (TextView) this.findViewById(R.id.textview_count);
		mAddressText = (TextView) this.findViewById(R.id.textview_address);
		mContactNameText = (TextView) this.findViewById(R.id.textview_contact_name);
		mPhoneText = (TextView) this.findViewById(R.id.textview_phone);
		mTimeText = (TextView) this.findViewById(R.id.textview_time);
		mYaoqiuText = (TextView) this.findViewById(R.id.textview_yaoqiu);
		
		mNextButton = (Button) this.findViewById(R.id.button_next);
	}

	private void setUpView() {
		mTopbarView.setCenterText("采购详情");
		mTopbarView.setLeftView(true, true);
		mTopbarView.setRightText("取消订单");
        mTopbarView.setRightClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                showCancelDialog();
            }
        });
		mPriceText.setVisibility(View.GONE);

		mNextButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
            	if(data!= null){
            		Intent intent = new Intent(PurchaseRecordDetailActivity.this,QuoteListActivity.class);
            		intent.putExtra(ExtraName.id, data.getOrderid());
            		startActivity(intent);
            	}
            }
        });
	}
	
	private void showCancelDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认取消订单?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                cancelQuote();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
    
    protected void cancelQuote() {
        startLoadingDialog();
        RequestService.getInstance().cancelPurchaseOrder(this, data.getOrderid(), BaseEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    showToast("取消订单成功");
//                    mTopbarView.setRightText("");
//                    mNextButton.setVisibility(View.GONE);
                    finish();
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }
	
}
